package com.easyskool.composer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONException;
import org.json.JSONObject;

import com.easyskool.constants.CommonConstants;
import com.easyskool.constants.TableNames;
import com.easyskool.dao.BaseDao;
import com.easyskool.exception.ComposerException;
import com.easyskool.exception.DaoException;
import com.easyskool.utilfunction.NullObjects;
import com.easyskool.utilfunction.UtilFunctions;
import com.ivp.cloud.stratus.logger.LoggingUtil;

public class AdminComposer {
	private static LoggingUtil logger = (LoggingUtil) LoggingUtil
			.getLogger(WebadminComposer.class.getName());

	BaseDao baseDao;

	public BaseDao getBaseDao() {
		if (NullObjects.isNull(baseDao)) {
			baseDao = new BaseDao();
		}
		logger.info("exiting getBaseDao");
		return baseDao;
	}

	// --------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param user
	 * @param toInsert
	 * @return String
	 * @throws Exception
	 */
	public String insertAdmin(String user, String toInsert) throws Exception {

		getBaseDao().insert(TableNames.adminDetails, toInsert);
		logger.info("exiting insertAdmin");
		return UtilFunctions.createJsonObject(CommonConstants.CREATE,
				CommonConstants.SUCCESS);
	}

	// --------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param user
	 * @param whereClause
	 * @return String
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String getAdmin(String user, String whereClause) throws Exception {

		ObjectMapper mapper = new ObjectMapper();

		Map whereMap = mapper.readValue(whereClause, HashMap.class);
		List<AdminDetails> admins = getBaseDao().find(TableNames.adminDetails,
				whereMap);
		String str = mapper.writeValueAsString(admins);
		logger.info("exiting getMasterClass");
		return str;
	}

	// --------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param user
	 * @param whereClause
	 * @param updateClause
	 * @return String
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String updateAdmin(String user, String whereClause,
			String updateClause) throws Exception {

		Map whereMap = new HashMap<>();
		getBaseDao().update(TableNames.adminDetails, whereMap, updateClause);
		logger.info("exiting updateMasterClass");
		return UtilFunctions.createJsonObject(CommonConstants.UPDATE,
				CommonConstants.SUCCESS);
	}

	// --------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param user
	 * @param whereClause
	 * @return String
	 * @throws Exception
	 */
	public String deleteAdmin(String user, String whereClause)
			throws Exception {

		ObjectMapper mapper = new ObjectMapper();

		AdminDetails whereMap = mapper.readValue(whereClause,
				TableNames.adminDetails);
		getBaseDao().delete(whereMap);
		logger.info("exiting deleteAdmin");
		return UtilFunctions.createJsonObject(CommonConstants.DELETE,
				CommonConstants.SUCCESS);
	}

	// --------------------------------------------------------------------------------------------------

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String deleteSelectedAdmin(String user, String whereClause)
			throws Exception {

		ObjectMapper mapper = new ObjectMapper();

		ArrayList<Map> whereMap = mapper
				.readValue(whereClause, ArrayList.class);
		for (Map map : whereMap) {
			String str = mapper.writeValueAsString(map);
			AdminDetails moderatorDetails = mapper.readValue(str,
					TableNames.adminDetails);
			getBaseDao().delete(moderatorDetails);
		}
		logger.info("exiting deleteSelectedAdmin");
		return UtilFunctions.createJsonObject(CommonConstants.DELETE,
				CommonConstants.SUCCESS);
	}

	public String insertTeacher(String user, String whereClause) throws JsonParseException, JsonMappingException, InstantiationException, IllegalAccessException, IOException, JSONException, DaoException {

		whereClause = UtilFunctions.insertParamAdd("Sumit",whereClause);
		System.out.println("where clause : "+whereClause);
		getBaseDao().insert(TableNames.teacherDetails, whereClause);
		logger.info("exiting insertTeacher");
		return UtilFunctions.createJsonObject(CommonConstants.CREATE,
				CommonConstants.SUCCESS);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String getTeachers(String user, String whereClause) throws Exception {

		ObjectMapper mapper = new ObjectMapper();

		
		
		
		Map whereMap = mapper.readValue(whereClause, HashMap.class);
		List<TeacherDetails> teachers = getBaseDao().find(TableNames.teacherDetails,whereMap);
		String str = mapper.writeValueAsString(teachers);
		logger.info("exiting getTeachers");
		return str;
		
//		for(TeacherDetails map : teachers){
//            Map<String, Object> m = new LinkedHashMap<>();
//            m.put("teacherCode", map.getTeacherCode());
//            m.put("tchFname", map.getSectionName());
//            m.put("sectionDesc", map.getSectionDesc());
//            m.put("createdBy", map.getCreatedBy());
//            m.put("createdDate", map.getCreatedDate());
//            m.put("modifiedBy", map.getModifiedBy());
//            m.put("modifiedDate", map.getModifiedDate());
//            
//            lmap.add(m);
//        }
//        String str = mapper.writeValueAsString(lmap);
//        logger.info("exiting getTeachers");
//        return str;
	}

	// --------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param user
	 * @param whereClause
	 * @param updateClause
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String updateTeacher(String user
							  , String whereClause
							  , String updateClause) 
			
									  throws Exception {

		Map whereMap = new HashMap<>();
		updateClause = UtilFunctions.updateParamAdd(user, updateClause);
		getBaseDao().update(TableNames.teacherDetails, whereMap, updateClause);
		logger.info("exiting updateTeacher");
		return UtilFunctions.createJsonObject(CommonConstants.UPDATE,
				CommonConstants.SUCCESS);
	}

	// --------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param user
	 * @param whereClause
	 * @return String
	 * @throws Exception
	 */
	public void deleteTeacher(String user
							  , String whereClause)
			throws Exception {

		try {
			ObjectMapper mapper = new ObjectMapper();
			Map<String, Object> whereMap = mapper.readValue(whereClause,
					HashMap.class);
System.out.println("here : ");
			getBaseDao().deleteWithoutFullObject(TableNames.teacherDetails, whereMap);
			logger.info("exiting deleteTeacher");
		} catch (DaoException e) {
			logger.error(e + ":" + e.getErrorMsg() + ":"
					+ e.getExthr().getMessage() + ":" + e.getExthr().getCause());

			ComposerException d = new ComposerException(e.getErrorMsg(), e);
			throw d;
		} catch (Exception e) {
			logger.error(e + e.getMessage() + ":" + e.getCause());

			ComposerException d = new ComposerException(e.getMessage(), e);
			throw d;
		}

	}

	// --------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param user
	 * @param whereClause
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void deleteSelectedTeachers(String user, String whereClause)
			throws Exception {

		ObjectMapper mapper = new ObjectMapper();
		try {
		
				Map<String, Object> whereMap = mapper.readValue(whereClause,
						HashMap.class);

				getBaseDao().batchDelete(TableNames.teacherDetails, whereMap);
				
			logger.info("exiting deleteSelectedTeachers");
		} catch (DaoException e) {
			logger.error(e + ":" + e.getErrorMsg() + ":"
					+ e.getExthr().getMessage() + ":" + e.getExthr().getCause());

			ComposerException d = new ComposerException(e.getErrorMsg(), e);
			throw d;
		} catch (Exception e) {
			logger.error(e + e.getMessage() + ":" + e.getCause());

			ComposerException d = new ComposerException(e.getMessage(), e);
			throw d;
		}
	}
	
	public String insertModerator(String user, String whereClause) throws JsonParseException, JsonMappingException, InstantiationException, IllegalAccessException, IOException, JSONException, DaoException {

		whereClause = UtilFunctions.insertParamAdd("Sumit",whereClause);
		System.out.println("where clause : "+whereClause);
		getBaseDao().insert(TableNames.moderatorDetails, whereClause);
		logger.info("exiting insertModerator");
		return UtilFunctions.createJsonObject(CommonConstants.CREATE,
				CommonConstants.SUCCESS);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String getModerators(String user, String whereClause) throws Exception {

		ObjectMapper mapper = new ObjectMapper();

		Map whereMap = null;
		
		if(!NullObjects.isNull(whereClause)){
		 whereMap = mapper.readValue(whereClause, HashMap.class);
		}
		List<ModeratorDetails> teachers = getBaseDao().find(TableNames.moderatorDetails,whereMap);
		String str = mapper.writeValueAsString(teachers);
		logger.info("exiting getModerators");
		return str;
		
//		for(ModeratorDetails map : teachers){
//            Map<String, Object> m = new LinkedHashMap<>();
//            m.put("teacherCode", map.getModeratorCode());
//            m.put("tchFname", map.getSectionName());
//            m.put("sectionDesc", map.getSectionDesc());
//            m.put("createdBy", map.getCreatedBy());
//            m.put("createdDate", map.getCreatedDate());
//            m.put("modifiedBy", map.getModifiedBy());
//            m.put("modifiedDate", map.getModifiedDate());
//            
//            lmap.add(m);
//        }
//        String str = mapper.writeValueAsString(lmap);
//        logger.info("exiting getModerators");
//        return str;
	}

	// --------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param user
	 * @param whereClause
	 * @param updateClause
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String updateModerator(String user
							  , String whereClause
							  , String updateClause) 
			
									  throws Exception {
System.out.println("inside composer");
		Map whereMap = new HashMap<>();
		updateClause = UtilFunctions.updateParamAdd(user, updateClause);
		getBaseDao().update(TableNames.moderatorDetails, whereMap, updateClause);
		logger.info("exiting updateModerator");
		return UtilFunctions.createJsonObject(CommonConstants.UPDATE,
				CommonConstants.SUCCESS);
	}

	// --------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param user
	 * @param whereClause
	 * @return String
	 * @throws Exception
	 */
	public void deleteModerator(String user
							  , String whereClause)
			throws Exception {

		try {
			ObjectMapper mapper = new ObjectMapper();
			Map<String, Object> whereMap = mapper.readValue(whereClause,
					HashMap.class);
System.out.println("here : ");
			getBaseDao().deleteWithoutFullObject(TableNames.moderatorDetails, whereMap);
			logger.info("exiting deleteModerator");
		} catch (DaoException e) {
			logger.error(e + ":" + e.getErrorMsg() + ":"
					+ e.getExthr().getMessage() + ":" + e.getExthr().getCause());

			ComposerException d = new ComposerException(e.getErrorMsg(), e);
			throw d;
		} catch (Exception e) {
			logger.error(e + e.getMessage() + ":" + e.getCause());

			ComposerException d = new ComposerException(e.getMessage(), e);
			throw d;
		}

	}

	// --------------------------------------------------------------------------------------------------
	/**
	 * 
	 * @param user
	 * @param whereClause
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void deleteSelectedModerators(String user, String whereClause)
			throws Exception {

		ObjectMapper mapper = new ObjectMapper();
		try {
		
				Map<String, Object> whereMap = mapper.readValue(whereClause,
						HashMap.class);

				getBaseDao().batchDelete(TableNames.moderatorDetails, whereMap);
				
			logger.info("exiting deleteSelectedTeachers");
		} catch (DaoException e) {
			logger.error(e + ":" + e.getErrorMsg() + ":"
					+ e.getExthr().getMessage() + ":" + e.getExthr().getCause());

			ComposerException d = new ComposerException(e.getErrorMsg(), e);
			throw d;
		} catch (Exception e) {
			logger.error(e + e.getMessage() + ":" + e.getCause());

			ComposerException d = new ComposerException(e.getMessage(), e);
			throw d;
		}
	}

	public List <ClassDetails> getClassSections(int schoolCode) {
		try {
			System.out.println("School Code entering in getClassSections Composer: "+schoolCode);
			Map<String, Object> whereMap = new HashMap<String, Object>();
			whereMap.put("schoolCode",schoolCode);
			List <ClassDetails> classSections=getBaseDao().find(ClassDetails.class, whereMap);
			if(classSections==null)
			{
				return null;
			}
			if(classSections.size()==0)
			{
				return null;
			}
			else
			{
				 
			}
			} catch (Exception e) {
				System.out.println("Exception in getClassSections Composer");
			e.printStackTrace();
		}
		return null;
		
	}
	
	
}
